package visao.locacao;

import visao.veiculo.InterfaceListagemVeiculosDiponiveis;
import java.awt.*;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import modelo.dominio.Cliente;
import modelo.dominio.Locacao;
import modelo.dominio.Motorista;
import modelo.dominio.TipoLocacao;
import modelo.dominio.Veiculo;
import modelo.persistencia.ControleAcesso;
import modelo.persistencia.PersistenciaCliente;
import modelo.persistencia.PersistenciaLocacao;
import modelo.persistencia.PersistenciaMotorista;
import modelo.persistencia.PersistenciaTipoLocacao;
import modelo.persistencia.PersistenciaVeiculo;

/*
 * Essa classe monta a Interface para alterar uma locação 
 */
public class InterfaceAlterarLocacao extends javax.swing.JFrame{
	
	private final PersistenciaLocacao persistenciaLocacao = new PersistenciaLocacao();
	private final PersistenciaMotorista persistenciaMotorista = new PersistenciaMotorista();
	private final PersistenciaVeiculo persistenciaVeiculo = new PersistenciaVeiculo();
	private final PersistenciaCliente persistenciaCliente = new PersistenciaCliente();
	private final PersistenciaTipoLocacao persistenciaTipoLocacao = new PersistenciaTipoLocacao();
	
	/*
	 * Construtor da classe Interface Alterar Locação
	 */
	public InterfaceAlterarLocacao(){
		initComponents();
		
		this.setTitle("Alterações em Locação");
		
		jButton1.setVisible(true);
		jButton2.setVisible(true);
		jButton3.setVisible(false);
		jButton4.setVisible(false);
		jButton5.setVisible(true);
		jButton6.setVisible(false);
		jLabel1.setVisible(true);
		jLabel2.setVisible(false);
		jLabel3.setVisible(false);
		jLabel4.setVisible(false);
		jLabel5.setVisible(false);
		jLabel6.setVisible(false);
		jPanel1.setVisible(true);
		jTextField1.setVisible(true);
		jTextField2.setVisible(false);
		jTextField3.setVisible(false);
		jTextField4.setVisible(false);
		jTextField5.setVisible(false);
		jTextField6.setVisible(false);
		
		
	}
	// fim do construtor
	
	@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
     private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        jTextField4 = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jButton5 = new javax.swing.JButton();
        jTextField5 = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jButton6 = new javax.swing.JButton();
        jTextField6 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Alterando Dados", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Ubuntu Light", 0, 18), new java.awt.Color(76, 76, 76))); // NOI18N

        jButton3.setText("Confirmar");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonConfirmarActionPerformed(evt);
            }
        });

        jButton4.setText("Cancelar");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelarActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Ubuntu Light", 0, 18)); // NOI18N
        jLabel1.setText("CPF : ");

        jLabel2.setFont(new java.awt.Font("Ubuntu Light", 0, 18)); // NOI18N
        jLabel2.setText("Placa : ");

        jLabel3.setFont(new java.awt.Font("Ubuntu Light", 0, 18)); // NOI18N
        jLabel3.setText("Tipo de Locação : ");

        jLabel4.setFont(new java.awt.Font("Ubuntu Light", 0, 18)); // NOI18N
        jLabel4.setText("Previsão de Dias : ");

        jLabel5.setFont(new java.awt.Font("Ubuntu Light", 0, 18)); // NOI18N
        jLabel5.setText("Km de saída : ");

        jButton5.setText("Ok");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonOkActionPerformed(evt);
            }
        });

        jLabel6.setFont(new java.awt.Font("Ubuntu Light", 0, 18)); // NOI18N
        jLabel6.setText("CNH : ");

        jButton6.setText("Listar Veículos");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
				try {
					jButtonListarVeiculosActionPerformed(evt);
				} catch (IOException ex) {
					Logger.getLogger(InterfaceAlterarLocacao.class.getName()).log(Level.SEVERE, null, ex);
				}
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(6, 6, 6)
                .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 288, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel5)
                            .addComponent(jLabel4)
                            .addComponent(jLabel3)
                            .addComponent(jLabel6))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jTextField6)
                            .addComponent(jTextField5)
                            .addComponent(jTextField4)
                            .addComponent(jTextField3)
                            .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 248, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(298, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(37, 37, 37)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                     .addComponent(jLabel1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton5))
                .addGap(39, 39, 39)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5)))
                    .addComponent(jLabel2))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(22, 22, 22)
                        .addComponent(jLabel6))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(59, 59, 59)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton3)
                    .addComponent(jButton4)
                    .addComponent(jButton6))
                .addContainerGap())
        );

        jButton1.setText("Logoff");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonLogoffActionPerformed(evt);
            }
        });

        jButton2.setText("Voltar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonVoltarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 537, Short.MAX_VALUE)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(49, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 479, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addGap(27, 27, 27))
        );

        pack();
    }// </editor-fold>
	/*
	 * Método do Botão Log Off que retorna para a janela de logoff
	 * @param evt
	 */
	private void jButtonLogoffActionPerformed(java.awt.event.ActionEvent evt) {
		dispose();
		ControleAcesso.logoff();
	}
	// fim do método
	
	/*
	 * Método do Botão Voltar que retorna para a janela de menu principal
	 * @param evt
	 */
	private void jButtonVoltarActionPerformed(java.awt.event.ActionEvent evt) {
		dispose();
		ControleAcesso.voltarMenuPrincipal();
	}
	//fim do método

	/*
	 * Método do Botão Cancelar, limpa os campos
	 * @param
	 */
	private void jButtonCancelarActionPerformed(java.awt.event.ActionEvent evt) {
		jButton1.setVisible(true);
		jButton2.setVisible(true);
		jButton3.setVisible(false);
		jButton4.setVisible(false);
		jButton5.setVisible(true);
		jButton6.setVisible(false);
		jLabel1.setVisible(true);
		jLabel2.setVisible(false);
		jLabel3.setVisible(false);
		jLabel4.setVisible(false);
		jLabel5.setVisible(false);
		jLabel6.setVisible(false);
		jPanel1.setVisible(true);
		jTextField1.setEnabled(true);
		jTextField1.setText("");
		jTextField1.setVisible(true);
		jTextField2.setText("");
		jTextField2.setVisible(false);
		jTextField3.setText("");
		jTextField3.setVisible(false);
		jTextField4.setText("");
		jTextField4.setVisible(false);
		jTextField5.setText("");
		jTextField5.setVisible(false);
		jTextField6.setText("");
		jTextField6.setVisible(false);
	}
	//fim do método 

	/*
	 * Método do Botão Confirmar, seta os campos de alteração sendo a pesquisa 
	 * realizada de cliente, veiculo e motorista caso não exista esse retorna
	 * uma msg de erro.
	 * @param evt
	 */
	private void jButtonConfirmarActionPerformed(java.awt.event.ActionEvent evt) {
		String pDias = jTextField3.getText();
		String kmSaida = jTextField4.getText();
		String cnh = jTextField6.getText();
		String idTV = jTextField3.getText();
		String placa = jTextField5.getText();
		String cpf = jTextField1.getText();
		
		if(pDias.equals("") && kmSaida.equals("") && cnh.equals("") && idTV.equals("") && placa.equals("")){
			JOptionPane.showMessageDialog(null,"<html> <font color='black'>Por favor, preencha pelo menos um campo!","Erro",JOptionPane.ERROR_MESSAGE);
		}else{
			long cpf2 = Long.parseLong(cpf);
			int id = Integer.parseInt(idTV);
			Motorista mot = new Motorista(cnh);
			Motorista mot1 = persistenciaMotorista.consultarMotoristaCNH(mot);
			if(mot1 != null){
				Veiculo vec = new Veiculo(placa);
				Veiculo vec1 = (Veiculo) persistenciaVeiculo.consultar(vec);
				if(vec1 != null){
					Cliente c = new Cliente(cpf2);
					Locacao loc = new Locacao(c);
					Locacao loc1 = persistenciaLocacao.consultarLocacaoVeiculo(loc);
					if(loc1 != null){
						TipoLocacao tipoLoc = new TipoLocacao(id);
						if(!pDias.equals("")){
							loc1.setPrevisaoDias(Integer.parseInt(pDias));
						}
						if(!kmSaida.equals("")){
							loc1.setQuilomentragemSaida(Long.parseLong(kmSaida));
						}
						if(!cnh.equals("")){
							loc1.setMotorista(mot);
						}
						if(!placa.equals("")){
							loc1.setVeiculo(vec);
						}
						if(!idTV.equals("")){
							loc1.setTipoLocacao(tipoLoc);
						}
						jButton1.setVisible(true);
						jButton2.setVisible(true);
						jButton3.setVisible(false);
						jButton4.setVisible(false);
						jButton5.setVisible(true);
						jButton6.setVisible(false);
						jLabel1.setVisible(true);
						jLabel2.setVisible(false);
						jLabel3.setVisible(false);
						jLabel4.setVisible(false);
						jLabel5.setVisible(false);
						jLabel6.setVisible(false);
						jPanel1.setVisible(true);
						jTextField1.setEnabled(true);
						jTextField1.setText("");
						jTextField1.setVisible(true);
						jTextField2.setText("");
						jTextField2.setVisible(false);
						jTextField3.setText("");
						jTextField3.setVisible(false);
						jTextField4.setText("");
						jTextField4.setVisible(false);
						jTextField5.setText("");
						jTextField5.setVisible(false);
						jTextField6.setText("");
						jTextField6.setVisible(false);
						persistenciaLocacao.atualizar(loc1);
						JOptionPane.showMessageDialog(null,"<html> <font color='black'>Alteração concluída!","Parabéns!",JOptionPane.INFORMATION_MESSAGE);	
					}else{
						JOptionPane.showMessageDialog(null,"<html> <font color='black'>Não existe Cliente com esse CPF!","Erro",JOptionPane.ERROR_MESSAGE);
					}
				}else{
					JOptionPane.showMessageDialog(null,"<html> <font color='black'>Não existe Veículo com essa Placa!","Erro",JOptionPane.ERROR_MESSAGE);
				}
			}else{
				JOptionPane.showMessageDialog(null,"<html> <font color='black'>Não existe Motorista com essa CNH!","Erro",JOptionPane.ERROR_MESSAGE);
			}	
		}
	}
	// fim do método
	
	/*
	 * Método que Verifica se o cpf retorna um cliente para assim 
	 * realizar a alteração da locação
	 * @param evt 
	 */
	private void jButtonOkActionPerformed(java.awt.event.ActionEvent evt) {
		String cpf = jTextField1.getText();
		
		if(!cpf.equals("")){
			long cpf2 = Long.parseLong(cpf);
			Cliente c = new Cliente(cpf2);
			Cliente c1 = persistenciaCliente.consultarClienteNome(c);
			
			if(c1 != null){
				jButton1.setVisible(true);
				jButton2.setVisible(true);
				jButton3.setVisible(true);
				jButton4.setVisible(true);
				jButton5.setVisible(true);
				jButton6.setVisible(true);
				jLabel1.setVisible(true);
				jLabel2.setVisible(true);
				jLabel3.setVisible(true);
				jLabel4.setVisible(true);
				jLabel5.setVisible(true);
				jLabel6.setVisible(true);
				jPanel1.setVisible(true);
				jTextField1.setEnabled(false);
				jTextField1.setVisible(true);
				jTextField2.setVisible(true);
				jTextField3.setVisible(true);
				jTextField4.setVisible(true);
				jTextField5.setVisible(true);
				jTextField6.setVisible(true);
			}else{
				JOptionPane.showMessageDialog(null,"<html> <font color='black'>Nenhum Cliente com esse CPF!</font> </html>","Erro",JOptionPane.ERROR_MESSAGE);
				jButton1.setVisible(true);
				jButton2.setVisible(true);
				jButton3.setVisible(false);
				jButton4.setVisible(false);
				jButton5.setVisible(true);
				jButton6.setVisible(false);
				jLabel1.setVisible(true);
				jLabel2.setVisible(false);
				jLabel3.setVisible(false);
				jLabel4.setVisible(false);
				jLabel5.setVisible(false);
				jLabel6.setVisible(false);
				jPanel1.setVisible(true);
				jTextField1.setEnabled(true);
				jTextField1.setText("");
				jTextField1.setVisible(true);
				jTextField2.setText("");
				jTextField2.setVisible(false);
				jTextField3.setText("");
				jTextField3.setVisible(false);
				jTextField4.setText("");
				jTextField4.setVisible(false);
				jTextField5.setText("");
				jTextField5.setVisible(false);
				jTextField6.setText("");
				jTextField6.setVisible(false);
			}
		}else{
			JOptionPane.showMessageDialog(null,"<html> <font color='black'>Digite um CPF por favor!</font> </html>","Erro",JOptionPane.ERROR_MESSAGE);
		}
	}
	// fim do método
	
	/*
	 * Método que lista o veículos 
	 * @param evt 
	 * @param throws IOException
	 */
	private void jButtonListarVeiculosActionPerformed(java.awt.event.ActionEvent evt) throws IOException {
		new InterfaceListagemVeiculosDiponiveis().setVisible(true);
	}
	// fim do método 
	
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextField jTextField5;
    private javax.swing.JTextField jTextField6;
}
// fim da classe